const app = getApp()
let qCloud = require("../../index.js");

Page({
  /** 页面的初始数据 */
  data: {
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
    produceTaskId: "", // 生产任务ID
    upcountList: [], // 生产任务-上数详情
    processList: [], // 生产任务-工序列表
    upcountProcessList: [], // 员工上数工序列表
  },
  /** 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    let self = this;
    self.setData({
      produceTaskId: options.produceTaskId
    });

    // 查询工序的上数列表
    self.queryProcessUpcountList();
    // 查询生产任务的工序列表
    self.queryProduceTaskProcessList();
  },
  /** 查询工序的上数列表 */
  queryProcessUpcountList: function () {
    let self = this;

    let params = {
      produceTaskId: self.data.produceTaskId, // 生产任务ID
    }
    app.fetchApis(self, qCloud.api.workreport_queryProcessUpcountList, params, "POST", function (e) {
      let result = e.data;
      if (result.code == 200) {
        let data = result.data;

        let upcountProcessList = []; // 员工上数工序列表
        for (let i = 0; i < data.processList.length; i++) {
          let processInfo = data.processList[i];
          let upcountProcess = {
            processId: processInfo.processId, // 工序ID
            processName: processInfo.processName, // 工序名称
            unitPrice: processInfo.unitPrice, // 单价
            submitQuantity: processInfo.submitQuantity, // 已提交总量
            processTotal: processInfo.processTotal, // 工序总量
            unfinishedQuantity: processInfo.unfinishedQuantity, // 未完成数量
            completedQuantity: processInfo.completedQuantity, // 完成数量
            submitTotal: 0, // 提交总数
          };
          upcountProcessList.push(upcountProcess);
        }

        self.setData({
          upcountProcessList: upcountProcessList
        });
      }
    });
  },
  /** 查询生产任务的工序列表 */
  queryProduceTaskProcessList: function () {
    let self = this;

    let params = {
      produceTaskId: self.data.produceTaskId, // 生产任务ID
    }
    app.fetchApis(self, qCloud.api.producetask_queryProduceTaskProcessList, params, "POST", function (e) {
      let result = e.data;
      if (result.code == 200) {
        let data = result.data;

        let upcountList = []; // 生产任务-上数详情
        let processList = []; // 生产任务-工序列表
        for (let i = 0; i < data.produceTaskProcessList.length; i++) {
          let produceTaskProcessInfo = data.produceTaskProcessList[i];

          if (produceTaskProcessInfo.completedProcess > 0) {
            let upcount = {
              produceTaskProcessId: produceTaskProcessInfo.produceTaskProcessId, // 生产任务工序ID
              processId: produceTaskProcessInfo.processId, // 工序ID
              processName: produceTaskProcessInfo.processName, // 工序名称
              processTotal: produceTaskProcessInfo.processTotal, // 工序总量
              unfinishedProcess: produceTaskProcessInfo.unfinishedProcess, // 未完成工序数量
              completedProcess: produceTaskProcessInfo.completedProcess, // 完成工序数量
              unitPrice: produceTaskProcessInfo.unitPrice, // 单价
              serialNumber: produceTaskProcessInfo.serialNumber, // 序号
            };
            upcountList.push(upcount);
          }

          let process = {
            produceTaskProcessId: produceTaskProcessInfo.produceTaskProcessId, // 生产任务工序ID
            processId: produceTaskProcessInfo.processId, // 工序ID
            processName: produceTaskProcessInfo.processName, // 工序名称
            processTotal: produceTaskProcessInfo.processTotal, // 工序总量
            unfinishedProcess: produceTaskProcessInfo.unfinishedProcess, // 未完成工序数量
            completedProcess: produceTaskProcessInfo.completedProcess, // 完成工序数量
            unitPrice: produceTaskProcessInfo.unitPrice, // 单价
            serialNumber: produceTaskProcessInfo.serialNumber, // 序号
          };
          processList.push(process);
        }

        self.setData({
          upcountList: upcountList,
          processList: processList
        });
      }
    });
  },
  /** 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {},
  /** 生命周期函数--监听页面显示 */
  onShow: function () {},
  /** 生命周期函数--监听页面隐藏 */
  onHide: function () {},
  /** 生命周期函数--监听页面卸载 */
  onUnload: function () {},
  /** 页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {},
  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function () {},
  /** 用户点击右上角分享 */
  onShareAppMessage: function () {}
});
